<template>
	<view>
		<image src="../../static/images/c3.jpg" mode="widthFix"></image>
		<view class="box">
			<view class="item mb">
				<view class="color1">
					<text>您的身份：</text>
					<text v-if="memberLevels.length==0">普通会员</text>
					<text v-else v-for="l in memberLevels" :key="index" style="margin-right: 10rpx;">{{l.name}}</text>
				</view>
				<view class="color1">每日任务可领：{{number}}</view>
			</view>
			<view class="item" v-for="(l,index) in levels" :key="index" :class="{mb:index<levels.length-1}">
				<view class="">{{l.name}}<text class="price">￥{{l.price}}</text></view>
				<view class="">每日任务可领：<text class="color2">{{l.taskNumber}}</text></view>
				<view class="">预计年收益为：<text class="color3">￥{{l.income}}</text></view>
				<view class="btn" v-if="isBuy(l)" @tap="openBuy(l)">立即开通</view>
			</view>
		</view>
		<u-modal v-model="show" content="确定购买？" :show-title="false" :show-cancel-button="true" @confirm="buyLevel"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				levels:[],
				memberLevels:[],
				show:false,
				okid:""
			};
		},
		onShow() {
			this.loadData();
		},
		computed:{
			number(){
				let n = 3;
				for (let l of this.memberLevels) {
					n = n+l.taskNumber;
				}
				return n;
			}
		},
		methods:{
			loadData(){
				this.$request.post("/getLevel", {
					dataType: 'json',
					responseType: 'text'
				})
				.then(res => {
					this.levels = res.data.json.levels;
					this.memberLevels = res.data.json.memberLevels;
				})
				.catch(error => {
				});
			},
			isBuy(target){
				for (let s of this.memberLevels) {
					if(s.id==target.id){
						return false;
					}
				}
				return true;
			},
			openBuy(target){
				this.show=true;
				this.okid = target.id;
			},
			buyLevel(){
				uni.showLoading({
					title:"购买中",
					mask:true
				})
				this.$request.post("/level/buy", {
					data:{id:this.okid},
					dataType: 'json',
					responseType: 'text'
				})
				.then(res => {
					uni.hideLoading();
					this.$api.msg("购买成功");
					this.show = false;
					this.loadData();
				})
				.catch(error => {
					uni.hideLoading();
				});
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #4b1cc4;
	}
	image{
		width: 100%;
		height: 100%;
	}
	.mb{
		margin-bottom: 30rpx;
	}
	.box{
		margin: 0 30rpx;
		padding: 30rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
	}
	.item{
		background-color: #a27ffb;
		padding: 30rpx;
		line-height: 50rpx;
		border-radius: 6rpx;
		font-size: 26rpx;
		position: relative;
	}
	.price{
		margin-left: 10rpx;
		color: #333333;
		font-size: 50rpx;
		color: #8f12a9;
	}
	.color1{
		color: #4b1cc4;
	}
	.color2{
		color: #C30BB8;
	}
	.color3{
		color: #992757;
	}
	.btn{
		position: absolute;
		right: 50rpx;
		top: 30%;
		background-color: #4b1cc4;
		padding: 7rpx 30rpx;
		border-radius: 30rpx;
		color: #FFFFFF;
	}
</style>
